﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LMS.SMSH.MODEL;
using LMS.SMSH.BLL;
using System.Data.Linq;
using System.Data;

namespace LMS.SMSH.WEB
{
    public partial class AdmStuManage : System.Web.UI.Page
    {
        int rowNo = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // 绑定GvStudentList数据
                bingGvStudentList();
            }
        }

        /// <summary>
        /// 绑定GvStudentList数据
        /// </summary>
        private void bingGvStudentList()
        {
            var query = from stu in LMSDataContext.GetLMSDB.TB_Student
                        select new
                        {
                            stu.stuNo,
                            stu.stuName,
                            stu.stuGender,
                            stu.stuPwd
                        };
            gvStudentList.DataSource = query;
            gvStudentList.DataKeyNames = new string[] { "stuNo" };
            gvStudentList.DataBind();

        }

        /// <summary>
        /// 根据学生姓名查找信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnFind_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtStudentName.Text.Trim()))
            {
                bingGvStudentList();
            }
            else
            {
                var query = from stu in LMSDataContext.GetLMSDB.TB_Student
                            where stu.stuName.IndexOf(txtStudentName.Text.Trim()) >= 0
                            select new
                            {
                                stu.stuNo,
                                stu.stuName,
                                stu.stuGender,
                                stu.stuPwd
                            };
                gvStudentList.DataSource = query;
                gvStudentList.DataKeyNames = new string[] { "stuNo" };
                gvStudentList.DataBind();
            }
        }

        /// <summary>
        /// 行数据绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvStudentList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                rowNo++;
                e.Row.Cells[0].Text = (gvStudentList.PageIndex * gvStudentList.PageSize + rowNo).ToString();
                //鼠标悬停变色
                e.Row.Attributes.Add("onmouseover", "oldBG=this.style.backgroundColor;this.style.backgroundColor='#ffeeaa';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=oldBG;");


                Button myDelete = (Button)e.Row.FindControl("btnMyDelete");
                myDelete.Attributes.Add("onclick", "javascript:return confirm('确定删除吗?');");
            }
        }

        /// <summary>
        /// 行命令
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvStudentList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "myDelete")
            {
                Button myDelete = (Button)e.CommandSource;
                myDelete.Attributes.Add("onclick", "javascript:return confirm('确定删除吗?');");
            }
        }

        /// <summary>
        /// 具体的删除数据行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnMyDelete_Click(object sender, EventArgs e)
        {
            Int32? result = -1;
            foreach (GridViewRow gr in gvStudentList.Rows)
            {
                CheckBox chkItem = (CheckBox)gr.FindControl("chkItem");
                if (chkItem.Checked)
                {
                    LMSDataContext.GetLMSDB.sp_delStudent(gr.Cells[2].Text.Trim(), ref result);
                }
            }
            bingGvStudentList();//再次绑定数据
        }

        /// <summary>
        /// 全选、反选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void chkHeader_CheckedChanged(object sender, EventArgs e)
        {
            foreach (GridViewRow gr in gvStudentList.Rows)
            {
                CheckBox chkItem = (CheckBox)gr.FindControl("chkItem");
                //相互变幻
                chkItem.Checked = !chkItem.Checked;
            }
        }

        /// <summary>
        /// 添加学生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnNew_Click(object sender, EventArgs e)
        {
            Response.Redirect("AdminStuAdd.aspx");
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvStudentList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvStudentList.PageIndex = e.NewPageIndex;
            bingGvStudentList();
        }




    }
}